import traceback

from selenium import webdriver
import os
import time
import re
from bs4 import BeautifulSoup
import random
import requests

from selenium.webdriver.common.by import By
import mysql_connect
from ShaneEssay import ShaneEssay
from ZhihuUser import ZhihuUser
from SnowflakeGenerator import SnowflakeGenerator

generator = SnowflakeGenerator(worker_id=1, datacenter_id=1)


def get_essay_id_list() -> list[int]:
    conn, cursor = mysql_connect.get_conn()
    essay_id_list = []
    try:

        sql = """
            SELECT
        id
    FROM
        shane_essay
    WHERE
        plain_text = ''
            
        """
        cursor.execute(sql)
        result = cursor.fetchall()
        essay_id_list = [essay_id[0] for essay_id in result]
    except:
        traceback.print_exc()
    finally:
        cursor.close()
        conn.close()
    return essay_id_list


def extract_one_data(file_name: str):
    html_text = ""
    shane_essay = ShaneEssay()
    shane_essay.id = int(file_name.split(".")[0])
    with open(f"other_files/question_title_htmls/title_htmls/{file_name}", "r", encoding="utf-8") as r_file:
        html_text = r_file.read()
    soup = BeautifulSoup(html_text, "html.parser")
    soup_title = soup.find("h1", {"class": "Post-Title"})
    # print(soup_title)
    shane_essay.title = soup_title.text
    soup_content = soup.find("div", {"class": "RichText ztext Post-RichText css-1g0fqss", "options": "[object Object]"})
    # print(soup_content)
    shane_essay.content = soup_content
    shane_essay.plain_text = beautify_text(soup_content.get_text())
    shane_essay.display_text = shane_essay.plain_text[0:100]

    zhihu_user = ZhihuUser()
    # soup_username = soup.find("a",{"target": "_blank", "data-za-detail-view-element_name": "User"})
    # print(soup_username)
    soup_username = soup.find("img", {"class": "Avatar AuthorInfo-avatar css-1y9jkzv"})
    # print(soup_username)
    zhihu_user.username = soup_username["alt"]
    zhihu_user.image_url = soup_username["src"]
    zhihu_user.zhihu_essay_id = file_name.split(".")[0]

    soup_introduction = soup.find("div", {"class": "ztext AuthorInfo-badgeText css-0"})
    zhihu_user.introduction = soup_introduction

    return shane_essay, zhihu_user


def save_in_mysql(shane_essay: ShaneEssay, zhihu_user: ZhihuUser):
    conn, cursor = mysql_connect.get_conn()
    try:
        sql_update_essay = """
            update shane_essay
                set title = %s,
                content = %s,
                plain_text = %s,
                display_text = %s
            where 
                id = %s
        """
        cursor.execute(sql_update_essay,
                       (shane_essay.title, shane_essay.content, shane_essay.plain_text, shane_essay.display_text,
                        shane_essay.id)
                       )
        sql_insert_user = """
            INSERT INTO zhihu_user 
	(id,username,introduction,image_url,zhihu_essay_id)
VALUES
	(%s,%s,%s,%s,%s)
        """
        cursor.execute(sql_insert_user,
                       (generator.generate_id(), zhihu_user.username, zhihu_user.introduction, zhihu_user.image_url,
                        zhihu_user.zhihu_essay_id)
                       )
        conn.commit()
    except:
        traceback.print_exc()
    finally:
        cursor.close()
        conn.close()


def extract_datas():
    file_name_list = os.listdir("other_files/question_title_htmls/title_htmls")
    essay_id_list = get_essay_id_list()

    for index, file_name in enumerate(file_name_list):
        # print(generator.generate_id())
        print(f"第{index}个，id为{file_name}，总共有{len(file_name_list)}个")
        if int(file_name.split(".")[0]) not in essay_id_list:
            print("已跳过")
            continue

        shane_essay: ShaneEssay
        zhihu_user: ZhihuUser
        try:
            shane_essay, zhihu_user = extract_one_data(file_name)
            save_in_mysql(shane_essay,zhihu_user)
        except:
            print("大失败！")
            traceback.print_exc()
            continue



def beautify_text(text: str):
    # 去除多余的空白字符和换行符
    text = re.sub('\s+', ' ', text)

    # 将文本按段落划分
    paragraphs = text.split('\n\n')

    # 重新格式化文本的缩进
    formatted_text = ''
    indent = ' ' * 4
    for paragraph in paragraphs:
        paragraph = indent + paragraph.strip()
        formatted_text += paragraph + '\n\n'

    return formatted_text.strip()


if __name__ == "__main__":
    extract_datas()
    print("12310"[0:4])
